package com.molichuxing.services.business.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.business.convertor.Convert;
import com.molichuxing.services.business.dto.request.modify.StockCarBizModifyDto;
import com.molichuxing.services.business.dto.request.modify.StockCarRejectionBizModifyDto;
import com.molichuxing.services.business.dto.response.StockCarBaseBizDto;
import com.molichuxing.services.business.dto.response.StockCarBizDto;
import com.molichuxing.services.business.dto.response.StockCarDetailDto;
import com.molichuxing.services.business.service.StockCarBizService;
import com.molichuxing.services.infrastructure.dto.request.create.StockRecordCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.StockCarModifyDto;
import com.molichuxing.services.infrastructure.dto.response.EntrepotStorageDto;
import com.molichuxing.services.infrastructure.dto.response.StockCarDownloadDto;
import com.molichuxing.services.infrastructure.dto.response.StockCarDto;
import com.molichuxing.services.infrastructure.dto.response.StockStoreDto;
import com.molichuxing.services.infrastructure.service.EntrepotStorageService;
import com.molichuxing.services.infrastructure.service.StockCarService;
import com.molichuxing.services.infrastructure.service.StockRecordService;
import com.molichuxing.services.infrastructure.service.StockStoreService;
import com.molichuxing.services.property.StockCarStatusEnum;
import com.molichuxing.services.property.StockCarStockTypeEnum;
import com.molichuxing.services.property.StockCarTypeEnum;
import com.molichuxing.services.property.StockRecordStatusEnum;
import com.molichuxing.services.property.StockRecordTypeEnum;

/**
 * 
 * @author liyg
 * 库存车辆组合服务实现类
 * 2019-08-22
 *
 */
@Service("stockCarBizService")
public class StockCarBizServiceImpl implements StockCarBizService{

	@Autowired
	private StockCarService stockCarService;
	
	@Autowired
	private EntrepotStorageService entrepotStorageService;
	
	@Autowired
	private StockStoreService stockStoreService;
	
	@Autowired
	private StockRecordService stockRecordService;
	
	/**
	 *库存车辆列表
	 * @param map 参数
	 *key:carIds      List      车辆ids 非必填
	 *key:vehicleId  Integer 车型id 非必填
	 *key:status     Integer 库存状态 非必填
	 *key:type       Integer 出入库类型 非必填
	 *key:operationState Integer   运营状态 非必填
	 *key:inStockType  Integer 收车库类型 非必填
	 *key:stockId    Integer 中转库id 非必填
	 *key:startTime  LocalDateTime 开始时间 非必填
	 *key:endTime    LocalDateTime 结束时间 非必填
	 *key:sortName   String 排序字段 
	 *key:sortWay    String 排序方式 
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockCarBizDto> getStockCarList(Map<String, Object> map) {
		
		Paged<StockCarDto> stockCarList = stockCarService.getStockCarList(map);
	    
		Paged<StockCarBizDto> paged = getList(stockCarList);
		
		return paged;
	}
	
	/**
	 * 获取库存车辆详情
	 * 
	 * @param carId
	 * @return
	 */
	@Override
	public StockCarDetailDto getStockCarDetail(Integer carId) {
		
		StockCarDto stockCarDto = stockCarService.getStockCar(carId);
		StockCarDetailDto stockCarDetailDto = Convert.toStockCarDetailDto(stockCarDto);
		if(null == stockCarDetailDto) {
			return stockCarDetailDto;
		}
		
		//获取仓库名称
		getStockName(stockCarDetailDto);
		
		return stockCarDetailDto;
	}
	
	/**
	 * 获取仓库名称
	 * @param stockCarDetailDto
	 */
	private void getStockName(StockCarDetailDto stockCarDetailDto) {
		
		Integer outStockType = stockCarDetailDto.getOutStockType();
		Integer outStockId = stockCarDetailDto.getOutStockId();
		if(null != outStockType && null != outStockId) {
			if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == outStockType ) {
				/*查询中转库名称*/
				EntrepotStorageDto entrepotStorage = entrepotStorageService.getEntrepotStorage(outStockId);
				if(null != entrepotStorage) {
					stockCarDetailDto.setOutStockName(entrepotStorage.getShortName());
				}
				
			}else {
				/*查询门店id*/
				StockStoreDto stockStore = stockStoreService.getStockStore(outStockId);
				if(null != stockStore) {
					stockCarDetailDto.setOutStoreId(stockStore.getStoreId());
				}
			}	
			
		}
		
		Integer inStockType = stockCarDetailDto.getInStockType();
		Integer inStockId = stockCarDetailDto.getInStockId();
		if(null != inStockType && null != inStockId) {
			if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == inStockType ) {
				/*查询中转库名称*/
				EntrepotStorageDto entrepotStorage = entrepotStorageService.getEntrepotStorage(inStockId);
				if(null != entrepotStorage) {
					stockCarDetailDto.setInStockName(entrepotStorage.getShortName());
				}
				
			}else {
				/*查询门店id*/
				StockStoreDto stockStore = stockStoreService.getStockStore(inStockId);
				if(null != stockStore) {
					stockCarDetailDto.setInStoreId(stockStore.getStoreId());
				}
			}	
			
		}
	}
	
	/**
	 * 获取库存车辆详情
	 * 
	 * @param carId
	 * @return
	 */
	@Override
	public StockCarBizDto getStockCar(Integer carId) {
	
		StockCarDto stockCarDto = stockCarService.getStockCar(carId);
		StockCarBizDto stockCarBizDto = Convert.toStockCarBizDto(stockCarDto);
		if(null == stockCarBizDto) {
			return stockCarBizDto;
		}
		
       if(StockCarStatusEnum.WAIT_STOCK.getValue() == stockCarDto.getStatus()) {
			
    	   Integer outStockType = stockCarDto.getOutStockType();
			//获取库存名称
			getStockName(outStockType,stockCarBizDto,stockCarDto.getOutStockId());
			
		}else {
			
			Integer inStockType = stockCarDto.getInStockType();
			//获取库存名称
			getStockName(inStockType,stockCarBizDto,stockCarDto.getInStockId());
		}
		
		return stockCarBizDto;
	}
	
	/**
	 * 获取库存名称
	 * 
	 * @param stockType
	 * @param stockCarBaseBizDto
	 * @param stockCarDto
	 */
	private void getStockName(Integer stockType,StockCarBizDto stockCarBizDto,Integer stockId) {
		
		stockCarBizDto.setStockType(stockType);
		stockCarBizDto.setStockId(stockId);
		if(null == stockType || null == stockId) {
		      return;
		}
		
		if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockType ) {
			/*查询中转库名称*/
			EntrepotStorageDto entrepotStorage = entrepotStorageService.getEntrepotStorage(stockId);
			if(null != entrepotStorage) {
				stockCarBizDto.setStockName(entrepotStorage.getShortName());
			}
			
		}else {
			/*查询门店id*/
			StockStoreDto stockStore = stockStoreService.getStockStore(stockId);
			if(null != stockStore) {
				stockCarBizDto.setStoreId(stockStore.getStoreId());
			}
		}	
	}
	
	
	/**
	 * 获取库存车辆详情
	 * 
	 * @param carId
	 * @return
	 */
	@Override
	public StockCarBaseBizDto getStockBaseCar(Integer carId) {
		
		StockCarDto stockCarDto = stockCarService.getStockCar(carId);
		if(null == stockCarDto) {
			return null;
		}
		
		StockCarBaseBizDto stockCarBaseBizDto = new StockCarBaseBizDto();
		BeanUtils.copyProperties(stockCarDto, stockCarBaseBizDto);
		if(StockCarStatusEnum.WAIT_STOCK.getValue() == stockCarDto.getStatus()) {
			
			Integer outStockType = stockCarDto.getOutStockType();
			//获取库存名称
			getStockName(outStockType,stockCarBaseBizDto,stockCarDto.getOutStockId());
			
		}else {
			
			Integer inStockType = stockCarDto.getInStockType();
			//获取库存名称
			getStockName(inStockType,stockCarBaseBizDto,stockCarDto.getInStockId());
		}
    		
		return stockCarBaseBizDto;
	}
	
	/**
	 * 获取库存车辆详情
	 * 
	 * @param carIds
	 * @return
	 */
	@Override
	public Map<Integer, StockCarBizDto> getStockBaseCars(List<Integer> carIds) {
		
		if(null == carIds || carIds.isEmpty()) {
			return null;
		}
		List<StockCarDownloadDto> list = stockCarService.getByIds(carIds);
		if(null == list || list.isEmpty()) {
			return null;
		}
		
		Set<Integer> stockIds= new HashSet<>();
		Set<Integer> storeIds= new HashSet<>();
		for (StockCarDownloadDto dto : list) {
			Integer stockId = dto.getStockId();
			Integer stockType = dto.getStockType();
			if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockType && null != stockId) {
				stockIds.add(stockId);
			}else if(StockCarStockTypeEnum.STORE.getValue() == stockType && null != stockId){
				storeIds.add(stockId);
			}
		}
		List<Integer> stockIdsList = new ArrayList<>(stockIds);
		List<Integer> storeIdsList = new ArrayList<>(storeIds);
		Map<Integer, EntrepotStorageDto> entrepotMap = entrepotStorageService.getEntrepotStorageList(stockIdsList);
		Map<Integer, StockStoreDto> stockStoresMap = stockStoreService.getStockStores(storeIdsList);
		Map<Integer, StockCarBizDto> map=new HashMap<>(list.size());
		for (StockCarDownloadDto dto : list) {
			StockCarBizDto bizDto = new StockCarBizDto();
			BeanUtils.copyProperties(dto, bizDto); 
			
			Integer stockId = dto.getStockId();
			Integer stockType = dto.getStockType();
			if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockType && null != stockId) {
				EntrepotStorageDto storageDto= null == entrepotMap ? null : entrepotMap.get(stockId);
				bizDto.setStockName(storageDto == null ? null : storageDto.getShortName());
			}else if(StockCarStockTypeEnum.STORE.getValue() == stockType && null != stockId){
				StockStoreDto storeDto= null == stockStoresMap ? null : stockStoresMap.get(stockId);
				bizDto.setStoreId(storeDto == null ? null : storeDto.getStoreId());
			}
			
			map.put(dto.getCarId(), bizDto);
		}
		return map;
	}
	
	/**
	 * 获取库存名称
	 * 
	 * @param stockType
	 * @param stockCarBaseBizDto
	 * @param stockCarDto
	 */
	private void getStockName(Integer stockType,StockCarBaseBizDto stockCarBaseBizDto,Integer stockId) {
		
		stockCarBaseBizDto.setStockType(stockType);
		stockCarBaseBizDto.setStockId(stockId);
		if(null == stockType || null == stockId) {
		      return;
		}
		
		if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockType ) {
			/*查询中转库名称*/
			EntrepotStorageDto entrepotStorage = entrepotStorageService.getEntrepotStorage(stockId);
			if(null != entrepotStorage) {
				stockCarBaseBizDto.setStockName(entrepotStorage.getShortName());
			}
			
		}else {
			/*查询门店id*/
			StockStoreDto stockStore = stockStoreService.getStockStore(stockId);
			if(null != stockStore) {
				stockCarBaseBizDto.setStoreId(stockStore.getStoreId());
			}
		}	
		
	}
	
	/**
	 * 车辆入库
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Boolean modifyInStock(StockCarBizModifyDto modifyDto) {
		
		StockCarDto stockCarDto = stockCarService.getStockCar(modifyDto.getCarId());
		if(null == stockCarDto) {
			throw new BizException(101,"不存在该条信息");
		}
		
		StockCarModifyDto stockCarModifyDto=new StockCarModifyDto();
		BeanUtils.copyProperties(modifyDto, stockCarModifyDto);
		stockCarModifyDto.setId(stockCarDto.getId());
		stockCarModifyDto.setStatus(StockCarStatusEnum.YET_STOCK.getValue());
		stockCarModifyDto.setType(stockCarDto.getType());
		stockCarModifyDto.setInStockId(modifyDto.getStockId());
		stockCarModifyDto.setInStockType(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue());
		stockCarModifyDto.setOutStockId(stockCarDto.getOutStockId());
		stockCarModifyDto.setOutStockType(stockCarDto.getOutStockType());
		stockCarModifyDto.setOutOperationTime(stockCarDto.getOutOperationTime());
		stockCarModifyDto.setOutAccountAccount(stockCarDto.getOutAccountAccount());
		stockCarModifyDto.setOutAccountId(stockCarDto.getOutAccountId());
		
		Boolean	flag = stockCarService.modify(stockCarModifyDto);
		
		if(!flag) {
			return flag;
		}
		
		//保存库存记录 
		createStockRecord(modifyDto,stockCarDto);
		
		return flag;
	}
	
	/**
	 *保存库存记录 
	 * 
	 * @param modifyDto
	 * @param stockCarDto
	 */
	private void createStockRecord(StockCarBizModifyDto modifyDto,StockCarDto stockCarDto) {
		
		StockRecordCreateDto stockRecordCreateDto = new StockRecordCreateDto();
		BeanUtils.copyProperties(stockCarDto, stockRecordCreateDto);
		stockRecordCreateDto.setPdiUrl(null);
		stockRecordCreateDto.setStatus(StockRecordStatusEnum.IN_STOCK.getValue());
		stockRecordCreateDto.setOutOperationTime(stockCarDto.getOutOperationTime());
		stockRecordCreateDto.setOutAccountAccount(stockCarDto.getOutAccountAccount());
		stockRecordCreateDto.setOutAccountId(stockCarDto.getOutAccountId());
		
		//入库时间
		stockRecordCreateDto.setOperationTime(modifyDto.getStorageTime());
		stockRecordCreateDto.setAccountId(modifyDto.getAccountId());
		stockRecordCreateDto.setAccountAccount(modifyDto.getAccountAccount());
		stockRecordCreateDto.setInStockId(modifyDto.getStockId());
		stockRecordCreateDto.setInStockType(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue());
		stockRecordCreateDto.setPdiUrl(modifyDto.getPdiUrl());
		if(StockCarTypeEnum.PURCHASE.getValue() == stockCarDto.getType()) {
			
			stockRecordCreateDto.setType(StockRecordTypeEnum.PURCHASE_IN_STOCK.getValue());
		}else if(StockCarTypeEnum.CAR_BACK.getValue() == stockCarDto.getType()) {
			
			stockRecordCreateDto.setType(StockRecordTypeEnum.CAR_BACK_IN_STOCK.getValue());
		}else {
			
			stockRecordCreateDto.setType(StockRecordTypeEnum.ALLOT_IN_STOCK.getValue());
		}
		
		Boolean flag = stockRecordService.create(stockRecordCreateDto);
		if(!flag) {
			throw new RuntimeException();
		}
	}
	
	/**
	 * 车辆出库
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Boolean modifyOutStock(StockCarBizModifyDto modifyDto) {
		
		StockCarDto stockCarDto = stockCarService.getStockCar(modifyDto.getCarId());
		if(null == stockCarDto) {
			throw new BizException(101, "不存在该条信息");
		}
		StockCarModifyDto stockCarModifyDto=new StockCarModifyDto();
		BeanUtils.copyProperties(modifyDto, stockCarModifyDto);
		stockCarModifyDto.setId(stockCarDto.getId());
		stockCarModifyDto.setOutStockId(stockCarDto.getInStockId());
		stockCarModifyDto.setOutStockType(stockCarDto.getInStockType());
		stockCarModifyDto.setOutOperationTime(modifyDto.getStorageTime());
		stockCarModifyDto.setOutAccountAccount(modifyDto.getAccountAccount());
		stockCarModifyDto.setOutAccountId(modifyDto.getAccountId());
		stockCarModifyDto.setInStockId(modifyDto.getStockId());
		stockCarModifyDto.setInStockType(modifyDto.getStockType());
		
		Boolean flag = stockCarService.modify(stockCarModifyDto);
		if(!flag) {
			return flag;
		}
		
		//保存库存记录
		createOutStockRecord(stockCarModifyDto,stockCarDto);
		
		return flag;
	}
	
	/**
	 *保存库存记录 
	 * 
	 * @param modifyDto
	 * @param stockCarDto
	 */
	private void createOutStockRecord(StockCarModifyDto stockCarModifyDto,StockCarDto stockCarDto) {
		
		StockRecordCreateDto stockRecordCreateDto = new StockRecordCreateDto();
		BeanUtils.copyProperties(stockCarModifyDto, stockRecordCreateDto);
		stockRecordCreateDto.setVehicleId(stockCarDto.getVehicleId());
		stockRecordCreateDto.setCarId(stockCarDto.getCarId());
		stockRecordCreateDto.setStatus(StockRecordStatusEnum.OUT_STOCK.getValue());
		//入库时间
		stockRecordCreateDto.setOperationTime(stockCarModifyDto.getStorageTime());
		if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockRecordCreateDto.getInStockType()) {
			
			if(StockCarTypeEnum.ALLOT.getValue() == stockCarModifyDto.getType()) {
				stockRecordCreateDto.setType(StockRecordTypeEnum.ALLOT_OUT_STOCK.getValue());
			}else {
				stockRecordCreateDto.setType(StockRecordTypeEnum.COLLECT_IN_STOCK.getValue());
			}
			
		}else {
			stockRecordCreateDto.setType(StockRecordTypeEnum.ALLOT_IN_STORE.getValue());
			//如果库存状态为已入库 则库存记录状态变为 已入库状态
			if(StockCarStatusEnum.YET_STOCK.getValue() == stockCarModifyDto.getStatus()) {
				stockRecordCreateDto.setStatus(StockRecordStatusEnum.IN_STOCK.getValue());
			}
		}
		
		Boolean flag = stockRecordService.create(stockRecordCreateDto);
		if(!flag) {
			throw new RuntimeException();
		}
	}
	
	/**
	 * 车辆拒收 门店换车
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Boolean modifyRejectionStock(StockCarRejectionBizModifyDto modifyDto) {
		
		StockCarDto oldStockCarDto = stockCarService.getStockCar(modifyDto.getOldCarId());
		if(null == oldStockCarDto) {
			throw new BizException(101, "不存在该条信息");
		}
		
		Boolean flag =modifyRejectionStock(modifyDto,oldStockCarDto);
		if(!flag) {
			return flag;
		}
		
		//保存库存记录 
		StockRecordCreateDto stockRecordCreateDto = Convert.toStockRecordCreateDto(modifyDto,oldStockCarDto);
		flag = stockRecordService.create(stockRecordCreateDto);
		if(!flag) {
			throw new RuntimeException();
		}
		return flag;
	}
	
	/**
	 * 车辆拒收 该车辆回到原来的中转库
	 * @param modifyDto
	 * @param oldStockCarDto
	 * @return
	 */
	private Boolean modifyRejectionStock(StockCarRejectionBizModifyDto modifyDto,StockCarDto oldStockCarDto) {
		StockCarModifyDto stockCarModifyDto=new StockCarModifyDto();
		stockCarModifyDto.setId(oldStockCarDto.getId());
		stockCarModifyDto.setStatus(StockCarStatusEnum.YET_STOCK.getValue());
		stockCarModifyDto.setType(StockCarTypeEnum.REJECTION.getValue());
		stockCarModifyDto.setOutStockId(oldStockCarDto.getInStockId());
		stockCarModifyDto.setOutStockType(oldStockCarDto.getInStockType());
		stockCarModifyDto.setOutOperationTime(modifyDto.getStorageTime());
		stockCarModifyDto.setOutAccountAccount(modifyDto.getAccountAccount());
		stockCarModifyDto.setOutAccountId(modifyDto.getAccountId());
		stockCarModifyDto.setInStockId(modifyDto.getStockId());
		stockCarModifyDto.setInStockType(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue());
		stockCarModifyDto.setPdiUrl(oldStockCarDto.getPdiUrl());
		stockCarModifyDto.setFenceId(modifyDto.getFenceId());
		return stockCarService.modify(stockCarModifyDto);
	}
	
	
	/**
	 * 门店收车  (车到门店) 或 确认回库
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Boolean modifyCollectStoreStock(StockCarBizModifyDto modifyDto) {
		
		StockCarDto stockCarDto = stockCarService.getStockCar(modifyDto.getCarId());
		if(null == stockCarDto) {
			throw new BizException(101, "不存在该条信息");
		}
		
		StockCarModifyDto stockCarModifyDto=new StockCarModifyDto();
		BeanUtils.copyProperties(modifyDto, stockCarModifyDto);
		stockCarModifyDto.setId(stockCarDto.getId());
		stockCarModifyDto.setStatus(StockCarStatusEnum.YET_STOCK.getValue());
		stockCarModifyDto.setInStockId(stockCarDto.getInStockId());
		
		Boolean flag=false;
		if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockCarDto.getInStockType()) {
			stockCarModifyDto.setInStockId(modifyDto.getStockId());
			flag = stockCarService.modifyInStockId(stockCarModifyDto);
		}else {
			flag = stockCarService.modifyStatus(stockCarModifyDto);
		}
		
		if(!flag) {
			return flag;
		}
		
		//保存库存记录
		createCollectStoreStockRecord(modifyDto,stockCarDto);
		
		return flag;
	}
	
	/**
	 *保存库存记录 
	 * 
	 * @param modifyDto
	 * @param stockCarDto
	 */
	private void createCollectStoreStockRecord(StockCarBizModifyDto modifyDto,StockCarDto stockCarDto) {
		
		StockRecordCreateDto stockRecordCreateDto = new StockRecordCreateDto();
		BeanUtils.copyProperties(stockCarDto, stockRecordCreateDto);
		stockRecordCreateDto.setStatus(StockRecordStatusEnum.IN_STOCK.getValue());
		if(StockCarTypeEnum.ALLOT.getValue() == stockCarDto.getType()) {
			stockRecordCreateDto.setType(StockRecordTypeEnum.ALLOT_IN_STORE.getValue());
		}else {
			stockRecordCreateDto.setType(StockRecordTypeEnum.COLLECT_IN_STOCK.getValue());
		}
		
		if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockCarDto.getInStockType()) {
			stockRecordCreateDto.setInStockId(modifyDto.getStockId());
		}
		
		//入库时间
		stockRecordCreateDto.setOperationTime(modifyDto.getStorageTime());
		stockRecordCreateDto.setOrderCode(modifyDto.getOrderCode());
		stockRecordCreateDto.setAccountId(modifyDto.getAccountId());
		stockRecordCreateDto.setAccountAccount(modifyDto.getAccountAccount());
		stockRecordCreateDto.setOutOperationTime(stockCarDto.getOutOperationTime());
		stockRecordCreateDto.setOutAccountAccount(stockCarDto.getOutAccountAccount());
		stockRecordCreateDto.setOutAccountId(stockCarDto.getOutAccountId());
		
		Boolean flag = stockRecordService.create(stockRecordCreateDto);
		if(!flag) {
			throw new RuntimeException();
		}
	}
	
	/**
	 * 收车 车到中转库
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Boolean modifyCollectStock(StockCarBizModifyDto modifyDto) {
		
		StockCarDto stockCarDto = stockCarService.getStockCar(modifyDto.getCarId());
		if(null == stockCarDto) {
			throw new BizException(101, "不存在该条信息");
		}
		
		StockCarModifyDto stockCarModifyDto=new StockCarModifyDto();
		BeanUtils.copyProperties(modifyDto, stockCarModifyDto);
		stockCarModifyDto.setId(stockCarDto.getId());
		stockCarModifyDto.setStatus(StockCarStatusEnum.YET_STOCK.getValue());
		stockCarModifyDto.setType(StockCarTypeEnum.COLLECT.getValue());
		stockCarModifyDto.setOutStockId(stockCarDto.getInStockId());
		stockCarModifyDto.setOutStockType(stockCarDto.getInStockType());
		stockCarModifyDto.setOutOperationTime(modifyDto.getStorageTime());
		stockCarModifyDto.setOutAccountAccount(modifyDto.getAccountAccount());
		stockCarModifyDto.setOutAccountId(modifyDto.getAccountId());
		stockCarModifyDto.setInStockId(modifyDto.getStockId());
		stockCarModifyDto.setInStockType(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue());
			
		Boolean flag = stockCarService.modify(stockCarModifyDto);
		if(!flag) {
			return flag;
		}
		
		//保存库存记录 
		createCollectStockRecord(modifyDto,stockCarDto);
		
		return flag;
	}
	
	/**
	 *保存库存记录 
	 * 
	 * @param modifyDto
	 * @param stockCarDto
	 */
	private void createCollectStockRecord(StockCarBizModifyDto modifyDto,StockCarDto stockCarDto) {
		
		StockRecordCreateDto stockRecordCreateDto = new StockRecordCreateDto();
		stockRecordCreateDto.setCarId(stockCarDto.getCarId());
		stockRecordCreateDto.setVehicleId(stockCarDto.getVehicleId());
		stockRecordCreateDto.setStatus(StockRecordStatusEnum.IN_STOCK.getValue());
		stockRecordCreateDto.setType(StockRecordTypeEnum.COLLECT_IN_STOCK.getValue());
		stockRecordCreateDto.setOutStockId(stockCarDto.getInStockId());
		stockRecordCreateDto.setOutStockType(stockCarDto.getInStockType());
		stockRecordCreateDto.setOutOperationTime(modifyDto.getStorageTime());
		stockRecordCreateDto.setOutAccountAccount(modifyDto.getAccountAccount());
		stockRecordCreateDto.setOutAccountId(modifyDto.getAccountId());
		stockRecordCreateDto.setInStockId(modifyDto.getStockId());
		stockRecordCreateDto.setInStockType(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue());
		//入库时间
		stockRecordCreateDto.setOperationTime(modifyDto.getStorageTime());
		stockRecordCreateDto.setPdiUrl(modifyDto.getPdiUrl());
		stockRecordCreateDto.setOrderCode(modifyDto.getOrderCode());
		stockRecordCreateDto.setAccountId(modifyDto.getAccountId());
		stockRecordCreateDto.setAccountAccount(modifyDto.getAccountAccount());
		
		Boolean flag = stockRecordService.create(stockRecordCreateDto);
		if(!flag) {
			throw new RuntimeException();
		}
	}
	
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Boolean modifyAdvanceStock(StockCarBizModifyDto modifyDto) {
		StockCarDto stockCarDto = stockCarService.getStockCar(modifyDto.getCarId());
		if(null == stockCarDto) {
			throw new BizException(101, "不存在该条信息");
		}
		
		StockCarModifyDto stockCarModifyDto=new StockCarModifyDto();
		stockCarModifyDto.setCarId(modifyDto.getCarId());
		stockCarModifyDto.setId(stockCarDto.getId());
		stockCarModifyDto.setStatus(StockCarStatusEnum.WAIT_STOCK.getValue());
		stockCarModifyDto.setType(StockCarTypeEnum.CAR_BACK.getValue());
		stockCarModifyDto.setOutStockId(stockCarDto.getInStockId());
		stockCarModifyDto.setOutStockType(stockCarDto.getInStockType());
		stockCarModifyDto.setOutOperationTime(LocalDateTime.now());
		stockCarModifyDto.setOutAccountAccount(modifyDto.getAccountAccount());
		stockCarModifyDto.setOutAccountId(modifyDto.getAccountId());
		stockCarModifyDto.setInStockId(modifyDto.getStockId());
		stockCarModifyDto.setInStockType(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue());
			
		Boolean flag = stockCarService.modify(stockCarModifyDto);
		if(!flag) {
			return flag;
		}
		
		//保存库存记录 
		createAdvanceStockRecord(modifyDto,stockCarDto);
		
		return flag;
	}
	
	
	/**
	 *保存库存记录 
	 * 
	 * @param modifyDto
	 * @param stockCarDto
	 */
	private void createAdvanceStockRecord(StockCarBizModifyDto modifyDto,StockCarDto stockCarDto) {
		
		StockRecordCreateDto stockRecordCreateDto = new StockRecordCreateDto();
		stockRecordCreateDto.setCarId(stockCarDto.getCarId());
		stockRecordCreateDto.setVehicleId(stockCarDto.getVehicleId());
		stockRecordCreateDto.setStatus(StockRecordStatusEnum.OUT_STOCK.getValue());
		stockRecordCreateDto.setType(StockRecordTypeEnum.CAR_BACK_IN_STOCK.getValue());
		stockRecordCreateDto.setOutStockId(stockCarDto.getInStockId());
		stockRecordCreateDto.setOutStockType(stockCarDto.getInStockType());
		stockRecordCreateDto.setOutOperationTime(LocalDateTime.now());
		stockRecordCreateDto.setOutAccountAccount(modifyDto.getAccountAccount());
		stockRecordCreateDto.setOutAccountId(modifyDto.getAccountId());
		stockRecordCreateDto.setInStockId(modifyDto.getStockId());
		stockRecordCreateDto.setInStockType(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue());
		//入库时间
		stockRecordCreateDto.setOperationTime(LocalDateTime.now());
		stockRecordCreateDto.setPdiUrl(modifyDto.getPdiUrl());
		stockRecordCreateDto.setAccountId(modifyDto.getAccountId());
		stockRecordCreateDto.setAccountAccount(modifyDto.getAccountAccount());
		
		Boolean flag = stockRecordService.create(stockRecordCreateDto);
		if(!flag) {
			throw new RuntimeException();
		}
	}
	
	/**
	 *车辆调拨列表（经销商）
	 * @param map 参数
	 *key:carIds     List 车辆ids 非必填
	 *key:vehicleId  Integer 车型id 非必填
	 *key:storeId    Integer 门店id 非必填
	 *key:stockIds     List 仓库ids
	 *key:sortName   String 排序字段 
	 *key:sortWay    String 排序方式 
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockCarBizDto> getDealerStockCarList(Map<String, Object> map) {
		
		Integer storeId = ObjectCastUtil.castInteger(map.get("storeId"));
		if(null != storeId) {
			StockStoreDto stockStoreDto= stockStoreService.getStockStoreByStoreId(storeId);
			if(null != stockStoreDto) {
				map.put("stockId", stockStoreDto.getStockId());
			}
		}
		
		Paged<StockCarDto> stockCarList = stockCarService.getDealerStockCarList(map);
		
		Paged<StockCarBizDto> paged = getList(stockCarList);
		
		return paged;
	}
	
	/**
	 *到期还车列表（经销商）
	 * @param map 参数
	 *key:carIds      List 车辆ids 非必填
	 *key:type        Integer 入库类型（1：采购，2：调拨，3、拒收、4、收车） 非必填
	 *key:status      Integer 库存状态（1：待入库，2：已入库） 非必填
	 *key:inStockType Integer 收车库类型（1：中转库，2：门店） 非必填
	 *key:sortName   String 排序字段 
	 *key:sortWay    String 排序方式 
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockCarDetailDto> getReturnStockCarList(Map<String, Object> map) {

        Paged<StockCarDto> stockCarList = stockCarService.getReturnStockCarList(map);
        if(null == stockCarList) {
        	return null;
        }else if(stockCarList.isEmpty()) {
        	return new Paged<>(null,stockCarList.getTotal(),stockCarList.getPageSize());
        }
        
        List<StockCarDto> list = stockCarList.getList();
        List<StockCarDetailDto> stockCarDetailDtoList = Convert.toStockCarDetailDto(list);
        for (StockCarDetailDto stockCarDetailDto : stockCarDetailDtoList) {
        	//获取仓库名称
        	getStockName(stockCarDetailDto);
		}
		
        Long total=stockCarList.getTotal();
		Integer pageSize=stockCarList.getPageSize();
		Paged<StockCarDetailDto> page=new Paged<>(stockCarDetailDtoList,total,pageSize);
		return page;
	}
	
	/**
	 * 获取列表数据
	 * @param stockCarList
	 * @return
	 */
	private Paged<StockCarBizDto> getList(Paged<StockCarDto> stockCarList){
		
		if(null == stockCarList) {
			return null;
		}else if(stockCarList.isEmpty()) {
			return new Paged<>(null,stockCarList.getTotal(),stockCarList.getPageSize());
		}
		
		List<StockCarDto> stockCarDtoList = stockCarList.getList();
		List<StockCarBizDto> stockCarBizDtoList=new ArrayList<>();
		
		for (StockCarDto dto : stockCarDtoList) {
			StockCarBizDto stockCarBizDto = new StockCarBizDto();
			BeanUtils.copyProperties(dto, stockCarBizDto);
			 
			if(StockCarStatusEnum.WAIT_STOCK.getValue() == dto.getStatus()) {
				
				Integer outStockType = dto.getOutStockType();
				//获取库存名称
				getStockName(outStockType,stockCarBizDto,dto.getOutStockId());	
		    	  
			}else {
				
				Integer inStockType = dto.getInStockType();
				//获取库存名称
				getStockName(inStockType,stockCarBizDto,dto.getInStockId());
			}		
			
			stockCarBizDtoList.add(stockCarBizDto);
		}
		
		Long total=stockCarList.getTotal();
		Integer pageSize=stockCarList.getPageSize();
		Paged<StockCarBizDto> page=new Paged<>(stockCarBizDtoList,total,pageSize);
		
		return page;
	}
	
	/**
	 *车辆调拨
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	@Override
	public Boolean modifyAllotStock(StockCarBizModifyDto modifyDto) {
		
		StockCarDto stockCarDto = stockCarService.getStockCar(modifyDto.getCarId());
		if(null == stockCarDto) {
			throw new BizException(101, "不存在该条信息");
		}
		StockCarModifyDto stockCarModifyDto=new StockCarModifyDto();
		BeanUtils.copyProperties(modifyDto, stockCarModifyDto);
		stockCarModifyDto.setId(stockCarDto.getId());
		stockCarModifyDto.setType(StockCarTypeEnum.ALLOT.getValue());
		stockCarModifyDto.setOutStockId(stockCarDto.getInStockId());
		stockCarModifyDto.setOutStockType(stockCarDto.getInStockType());
		stockCarModifyDto.setOutOperationTime(modifyDto.getStorageTime());
		stockCarModifyDto.setOutAccountAccount(modifyDto.getAccountAccount());
		stockCarModifyDto.setOutAccountId(modifyDto.getAccountId());
		stockCarModifyDto.setInStockId(modifyDto.getStockId());
		stockCarModifyDto.setInStockType(modifyDto.getStockType());
		
		Boolean flag = stockCarService.modifyAllotStock(stockCarModifyDto);
		if(!flag) {
			return flag;
		}
		
		//保存库存记录
		createOutStockRecord(stockCarModifyDto,stockCarDto);
		
		return flag;
	}
	
	/**
	 *库存车辆列表(导出)
	 * @param map 参数
	 *key:ids        List 仓库ids
	 *key:carId      Integer      车辆id 非必填
	 *key:vehicleId  Integer 车型id 非必填
	 *key:status     Integer 库存状态 非必填
	 *key:type       Integer 出入库类型 非必填
	 *key:operationState Integer   运营状态 非必填
	 *key:inStockType  Integer 收车库类型 非必填
	 *key:stockId    Integer 中转库id 非必填
	 *key:startTime  LocalDateTime 开始时间 非必填
	 *key:endTime    LocalDateTime 结束时间 非必填
	 * @return
	 */
	@Override
	public List<StockCarBizDto> getStockCarDownloadList(Map<String, Object> map) {
		List<StockCarDownloadDto> list = stockCarService.getStockCarDownloadList(map);
		if(null == list || list.isEmpty()) {
			return null;
		}
		return getStockCarBizList(list);
	}
	
	/**
	 * 查询 待出租 并且 为已入库 的车辆
	 * @return
	 */
	@Override
	public List<StockCarBizDto> getStockListByStatus() {
		List<StockCarDownloadDto> list = stockCarService.getStockListByStatus();
		if(null == list || list.isEmpty()) {
			return null;
		}
		return getStockCarBizList(list);
	}
	
	/**
	 * 转换数据
	 * @param list
	 * @return
	 */
	private List<StockCarBizDto> getStockCarBizList(List<StockCarDownloadDto> list){
		Set<Integer> stockIds= new HashSet<>();
		Set<Integer> storeIds= new HashSet<>();
		for (StockCarDownloadDto dto : list) {
			Integer stockId = dto.getStockId();
			Integer stockType = dto.getStockType();
			if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockType && null != stockId) {
				stockIds.add(stockId);
			}else if(StockCarStockTypeEnum.STORE.getValue() == stockType && null != stockId){
				storeIds.add(stockId);
			}
		}
		List<Integer> stockIdsList = new ArrayList<>(stockIds);
		List<Integer> storeIdsList = new ArrayList<>(storeIds);
		Map<Integer, EntrepotStorageDto> entrepotMap = entrepotStorageService.getEntrepotStorageList(stockIdsList);
		Map<Integer, StockStoreDto> stockStoresMap = stockStoreService.getStockStores(storeIdsList);
		List<StockCarBizDto> bizDtos = new ArrayList<>(list.size());
		for (StockCarDownloadDto dto : list) {
			StockCarBizDto bizDto = new StockCarBizDto();
			BeanUtils.copyProperties(dto, bizDto); 
			
			Integer stockId = dto.getStockId();
			Integer stockType = dto.getStockType();
			if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockType && null != stockId) {
				EntrepotStorageDto storageDto= null == entrepotMap ? null : entrepotMap.get(stockId);
				bizDto.setStockName(storageDto == null ? null : storageDto.getShortName());
			}else if(StockCarStockTypeEnum.STORE.getValue() == stockType && null != stockId){
				StockStoreDto storeDto= null == stockStoresMap ? null : stockStoresMap.get(stockId);
				bizDto.setStoreId(storeDto.getStoreId());
			}
			
			bizDtos.add(bizDto);
		}
		return bizDtos;
	}
}
